package 二叉树的层序遍历;

import java.util.*;

/**
 * @author: zhengtao
 * @description: 二叉树的最小深度
 * @version: 1.0
 * @date: 2022/10/29  15:32
 */
public class Demo111 {
    public int minDepth(TreeNode root) {

        if(root==null){
            return 0;
        }
        Queue<TreeNode> queue=new ArrayDeque<>();
        queue.add(root);
        int count=0;
        while (!queue.isEmpty()){
            int size= queue.size();
            count++;
            while (size-->0){
                TreeNode poll = queue.poll();
                if(poll.left!=null){
                    queue.add(poll.left);
                }
                if(poll.right!=null){
                    queue.add(poll.right);
                }
                if(poll.right==null&&poll.left==null){
                    return  count;
                }
            }
        }
        return 0;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left,TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
